var layer;
var $;
var element;
var form;
var laydate;
var table;
layui.use(['element','layer','laydate','form','table'], function(){
    layer = layui.layer;
    $ = layui.jquery;
    element = layui.element;
    form = layui.form;
    laydate = layui.laydate;
    table = layui.table;
    selListTab();

})
/**
 *  点击确定获取数据
 * */
function subGiveAway(){
    var userId = $("input[name=userId]").val();
    var checkStatus = table.checkStatus('ListTab');
    var couponIds = [];
    $(checkStatus.data).each(function (i, o) {//o即为表格中一行的数据
        couponIds.push(o.couponId);
    });
    couponIds = couponIds.join(",");
    if (couponIds.length < 1) {
        layer.msg('请选择所要赠送的优惠券', {icon: 7});
        return false;
    }else{
        layer.confirm('您确定要发放优惠券吗?', function (index) {
            $.post("/personnel/addUserCoupons", {userId: userId, couponId: couponIds}, function (res) {
                if(res){
                    layer.msg("发放成功",{icon:1});
                    selListTab();
                }else{
                    layer.msg("发放失败",{icon:2});
                }
            });
        });
    }
}
function selListTab(){
    $.ajax({
        url:"/personnel/selAwayListTab",
        type:"get",
        dataType:"json",
        data:{},
        success:function(item) {
            var data = item.obj;
            showSelListTab(data);
        }
    })
}
function showSelListTab(item){
    table.render({
        elem:"#ListTab",//容器
        title:"用户数据列表",
        limit:6,//分页显示条数
        width:970,
        data:item,
        toolbar:true,
        page:true,
        cols:[[//定义列
            {type:'checkbox',align:'center',width:40},//复选框靠左
            {title:'序号',align:'center',width:60,templet:function(data){
                    return data.LAY_INDEX;
                }},
            {field:"couponCode",title:"优惠券编号",align:'center',width:110},
            {field:"couponName",title:"优惠券名称",align:'center',width:110},
            {field:"couponPrice",title:"优惠券金额",align:'center',width:100,templet:function(data){
                    return data.couponPrice+"元";
                }},
            {field:"startDate",title:"开始时间",align:'center',width:150},
            {field:"endDate",title:"结束时间",align:'center',width:150},
            {field:"surplus",title:"剩余数量",align:'center',width:120},
            {field:"type",title:"优惠券类型",align:'center',width:120,templet:function(data){
                    if(data.type==2){
                        return "商品券"
                    }else if(data.type==3){
                        return "视频券"
                    }else if(data.type==4){
                        return "通用券"
                    }
                }},
        ]]
    });
}
/**
 *  点击 取消关闭弹出层
 */
function closeLayer(){
    var layer=parent.layer;
    layer.close(layer.index);
}